package org.hwadee.mapper;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.hwadee.entity.Category;
import org.hwadee.entity.Post;
import org.hwadee.entity.User;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface PostMapper {

    @Update("update post set postStatus = 0 where categoryId = #{categoryId}")
    void deletePostByCategory(Category category);

    @Select("select * from post where categoryId != 1")
    List<Post> getAllPostss();

    @Select("select * from post where postStatus = 1 order by updateTime Desc")
    List<Post> getAllPosts();

    @Select("select * from post where postUserId = #{userId} and categoryId != 1 and postStatus = 1")
    List<Post> getPostsByUser(User user);

    @Insert("insert into post(postUserId, title, createTime, updateTime, postContent, postSummary, categoryId) values (#{postUserId}, #{title}, #{createTime}, #{updateTime}, #{postContent}, #{postSummary}, #{categoryId})")
    void insertPost(Post post);

    @Select("select * from post where categoryId = #{categoryId} and postStatus = 1")
    List<Post> listPostOnlyByCategoryId(Integer categoryId);

    @Select("select * from post where postId = #{id} and postStatus = #{i}")
    Post getPostByIdAndStatus(@Param("id") Integer id, @Param("i") int i);

    @Update("update post set postStatus = 0 where postId = #{postId}")
    void deletePost(Post post);

    @Update("update post set title = #{title}, updateTime = #{updateTime}, postContent = #{postContent}, postSummary = #{postSummary}, category = #{category} where postId = #{postId}")
    void updatePost(Post post);

    @Select("select * from post where category = #{categoryId} and postStatus = 1 limit #{i}")
    List<Post> listPostByCategoryId(Integer categoryId, int i);

    @Select("select * from post where postStatus = 1 order by postViewCount, postId limit #{i}")
    List<Post> listPostByViewCount(int i);

    @Select("select top 1 from post where postStatus = 1 and postId > #{postId} order by postId")
    Post getAfterPost(Integer postId);

    @Select("select top 1 from post where postStatus = 1 and postId < #{postId} order by postId")
    Post getPrePost(Integer postId);

    @Select("select top 1 from post where postStatus = 1 order by RAND() limit #{i}")
    List<Post> listRandomPost(int i);

    @Select("select * from post where postStatus = 1 order by postCommentCount, postId limit #{i}")
    List<Post> listPostByCommentCount(int i);

    @Update("update post set likeCount = #{likeCount} where postId = #{postId}")
    void updatePostLike(Post post);

    @Update("update post set postViewCount = #{postViewCount} where postId = #{postId}")
    void updatePostView(Post post);

    @Select("select * from post where title like #{keywords} and postStatus = 1")
    List<Post> getPostsByKeywords(String keywords);

    @Select("select * from post where postStatus = 1 order by updateTime Desc limit #{limit}")
    List<Post> listRecentPost(int limit);

    @Select("select * from post where postUserId = #{userId} and postStatus = 1 order by createTime desc limit #{i}")
    List<Post> listRecentPostById(@Param("userId") Integer userId, @Param("i")int i);

    @Select("select * from post where categoryId = #{categoryId} and postStatus = 1")
    List<Post> getPostsByCategoryId(Integer categoryId);

    @Update("update post set commentCount = #{commentCount} where postId = #{postId}")
    void updateCommentCount(Integer postId, Integer commentCount);

//    @Select("select count postId from post where postUserId = #{userId}")
    Integer countPostByUserId(Integer userId);

    @Select("select categoryId from post where postId = #{postId} and postStatus = 1")
    Integer getCategoryIdByPostId(Integer postId);

    @Select("select * from post where categoryId = 1")
    List<Post> getAllNotices();

    @Update("update post set postStatus = 0 where postId = #{id}")
    void deletePostById(Integer id);

    @Update("update post set postStatus = 1 where postId = #{id}")
    void recoveryPostById(Integer id);

    @Select("select * from post where postUserId = #{userId} and categoryId != 1 and postStatus = 1 and title like #{s}")
    List<Post> getPostsByUserAndSearch(@Param("userId")Integer userId, @Param("s")String s);

    @Select("select * from post where postUserId in #{userId} and categoryId != 1")
    List<Post> getAllPostsByUser(List<User> userList);

    @Select("select * from post where title like #{search} and categoryId != 1")
    List<Post> getPostsByTitle(String search);

    @Select("select * from post where postId = #{commentPostId} and postStatus = 1")
    Post getPostByPostId(Integer commentPostId);
}
